/* 2017/05/05 */
package org.sample.springboot.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ConvertAspect {
	
	@Autowired
	private ConvertManager convertManager;
	
	@Pointcut("execution(public * org.sample.springboot..*Controller.*(..))")
	public void restCall() {
		// Pointcut definition.
	}

	@Before("restCall()")
	public void preprocessRequest(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		for (Object arg : args) {
			if (arg != null) {
				System.out.println("Preprocess " + arg.getClass().getSimpleName());
				convertManager.convert(arg);
			}
		}
	}

	@AfterReturning(pointcut = "restCall()", returning = "result")
	public void postprocessResponse(JoinPoint joinPoint, Object result) {
		if (result != null) {
			System.out.println("Postprocess " + result.getClass().getSimpleName());
			convertManager.convert(result);
		}
	}

}
